package com.example.safeassistan.home.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.activity.RiskPointDetail;
import com.example.safeassistan.home.adapter.RiskDetailPersonFragmentAdapter;
import com.example.safeassistan.home.adapter.RiskDetailRiskFragmentAdapter;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.adapter.RiskMyListAdapter;
import com.example.safeassistan.risk.model.RiskPointListSearchModel;
import com.example.safeassistan.risk.model.RiskPointPersonModel;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.widget.ViewPagerForScrollView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.example.safeassistan.constants.Constants.PAGESIZE;

public class RiskDetailPersonFragment extends BaseVPFragment {
    private Context context;
    private View view;
    private RecyclerView rcView;

    private RiskDetailPersonFragmentAdapter adapter;
    private ViewPagerForScrollView pager;
    private String riskpointId="";


    public RiskDetailPersonFragment(ViewPagerForScrollView pager,String riskpointId) {
        this.pager = pager;
        this.riskpointId=riskpointId;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.riskdetail_table_fragment, container, false);
            pager.setObjectForPosition(view,6);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        rcView=view.findViewById(R.id.rcView);
        getData();
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {

    }

    // 获取数据
    public void getData(){
        new HttpRequest(context).doGet(UrlConstants.getRiskPortPerson+riskpointId, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), RiskPointPersonModel.class);
                        if (obj instanceof RiskPointPersonModel) {
                            RiskPointPersonModel model=(RiskPointPersonModel) obj;
                            adapter=new RiskDetailPersonFragmentAdapter(model.getResult());
                            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false){
                                @Override
                                public boolean canScrollVertically() {
                                    return false;
                                }
                            };
                            rcView.setLayoutManager(linearLayoutManager);
                            rcView.setAdapter(adapter);
                        }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }
}
